package itsm.isperp.framework.quartz.provider.impl;

import itsm.isperp.framework.quartz.domain.JobDefinition;
import itsm.isperp.framework.quartz.domain.JobState;
import itsm.isperp.framework.quartz.domain.RunJob;
import itsm.isperp.framework.quartz.provider.IRunJobProvider;
import itsm.isperp.framework.quartz.service.IJobService;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 系统启动时检查定义的任务是否状态为run的，如果有则进行启动
 * 
 * @author JETSONLEE
 * @since 1.0
 */
public class DefaultRunJobProvider implements IRunJobProvider {

	private final Log logger = LogFactory.getLog(getClass());

	private IJobService<? extends JobDefinition> jobService;

	public Collection<RunJob> getRunJobs() {
		List<RunJob> jobs = new ArrayList<RunJob>();
		List<? extends JobDefinition> jobDefinitions = jobService
				.loadJobs(JobState.run);
		logger.info("There are " + jobDefinitions.size()
				+ " jobs to run for startup");
		for (JobDefinition jobDef : jobDefinitions) {
			RunJob job = new RunJob();
			job.setJobDefinition(jobDef);
			jobs.add(job);
		}
		return jobs;
	}

	public IJobService<? extends JobDefinition> getJobService() {
		return jobService;
	}

	public void setJobService(IJobService<? extends JobDefinition> jobService) {
		this.jobService = jobService;
	}

}
